package org.example.teaView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class HandleChangeTopic implements ActionListener {
    ChangeTopicView changeTopicView;
    Socket socket=null;
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread;

    public void setChangeTopicView(ChangeTopicView changeTopicView) {
        this.changeTopicView = changeTopicView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == changeTopicView.back){
            changeTopicView.setVisible(false);
            new Firstview();
        }
        InetAddress address= null;
        socket=new Socket();
        thread=new Thread(changeTopicView);
        try {
            address = InetAddress.getByName("127.0.0.1");
            InetSocketAddress socketAddress=new InetSocketAddress
                    (address,4331);
            socket.connect(socketAddress);
            in =new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            thread.start();
        } catch (IOException ex) {
            socket=new Socket();
        }

        if (e.getSource() == changeTopicView.change) {
            if (changeTopicView.inputID.getText().trim().length() == 0) {
                changeTopicView.hintMess.setText("请输入修改题号");
                return;
            }
            String s = "##";
            s += changeTopicView.inputID.getText().trim() + "##";
            s += changeTopicView.inputName.getText().trim() + "##";
            s += changeTopicView.inputSelect_A.getText().trim() + "##";
            s += changeTopicView.inputSelect_B.getText().trim() + "##";
            s += changeTopicView.inputSelect_C.getText().trim() + "##";
            s += changeTopicView.inputSelect_D.getText().trim() + "##";
            s += changeTopicView.input_ans.getText().trim() + "##1";
            if (s.substring(3).equals("##############1")){
                changeTopicView.hintMess.setText("请输入要修改内容");
                return;
            }
            System.out.println(s);
            try {
                out.writeUTF(s);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
